package io.github.luons.mediator.dsl.handler;

import com.github.pagehelper.PageHelper;
import io.github.luons.mediator.dsl.view.FilterGroupsRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * request param holder
 *
 * @author : luons
 */
@Slf4j
public class RequestHolder {

    private final static ThreadLocal<CommonRequest> REQUEST_COMMON_HOLDER = new ThreadLocal<>();

    private final static ThreadLocal<FilterGroupsRequest> REQUEST_FILTER_MAP_HOLDER = new ThreadLocal<>();

    public static void addFilterGroupsRequest(FilterGroupsRequest filterGroup) {
        REQUEST_FILTER_MAP_HOLDER.set(filterGroup);
    }

    public static FilterGroupsRequest getFilterGroupsRequest() {
        FilterGroupsRequest filterGroups = REQUEST_FILTER_MAP_HOLDER.get();
        return Objects.isNull(filterGroups) ? null : filterGroups;
    }

    public static void removeFilterMapRequest() {
        REQUEST_FILTER_MAP_HOLDER.remove();
    }

    public static void addPageAbleDefault(int pageSize) {
        PageHelper.startPage(1, pageSize);
    }

    public static void addPageAble(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
    }

    public static void addCommonRequest(CommonRequest commonRequest) {
        REQUEST_COMMON_HOLDER.set(commonRequest);
    }

    public static CommonRequest getCommonRequest() {
        return REQUEST_COMMON_HOLDER.get();
    }

    public static void removeCommonRequest() {
        REQUEST_COMMON_HOLDER.remove();
    }

    public static void removeAllRequestHolder() {
        if (Objects.nonNull(RequestHolder.getFilterGroupsRequest())) {
            REQUEST_FILTER_MAP_HOLDER.remove();
            REQUEST_COMMON_HOLDER.remove();
        }
    }


}
